package com.speed_run.controller;

import com.speed_run.dto.CodeDTO;
import com.speed_run.dto.UserInfoDTO;
import com.speed_run.service.ICircleService;
import com.speed_run.service.IUserService;
import com.speed_run.utils.GlobalConst;
import com.speed_run.utils.ResponseEntity;
import com.speed_run.utils.SmsSenderUtil;
import com.speed_run.vo.ModifyPwdVo;
import com.speed_run.vo.SmsCodeVo;
import com.speed_run.vo.UserRegVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


//@Api(tags={"用户操作接口"})
@RestController
@RequestMapping("/v1/api/user")
@Log4j2
public class UserController {

	@Autowired
	private IUserService userService;


	@Autowired
	private ICircleService circleService;

	@PostMapping("/register")
	public ResponseEntity user(@RequestBody  UserRegVo user){
		return userService.addUserInfo(user);
	}


	@PostMapping("/edit")
	public ResponseEntity user(@RequestParam(value = "file",required = false) MultipartFile avator ,
							   @RequestParam(value = "album",required = false) MultipartFile[] album,  UserInfoDTO info){
		return userService.editUserInfo(info, avator,album);
	}


	@GetMapping("/smsCode")
	public ResponseEntity getSmsCode(@RequestParam(value="telNum", required = true) String telNum){
		Map<String, String> data = new HashMap<String, String>();
		int code = 0;
		try {
			code = (int)((Math.random()*9+1)*100000);
			SmsSenderUtil.sendSmsCode(telNum,code+"");
			GlobalConst.codeMap.put(telNum,new CodeDTO(code,System.currentTimeMillis()));
		} catch (Exception e) {
			e.printStackTrace();
			return  ResponseEntity.fail("程序异常,请稍后再试");
		}
		data.put("smsCode", code+"");
		return  ResponseEntity.ok("获取短息验证码成功",data);
	}

	/**
	 * TODO 实现校验功能
	 * @param vo
	 * @return
	 */
	@PostMapping("/smsCode")
	public ResponseEntity checkSmsCode(@RequestBody SmsCodeVo vo){
		CodeDTO dto= GlobalConst.codeMap.get(vo.getTelphone());
		if(dto == null){
			return ResponseEntity.fail("短信验证码非法");
		}
		if((System.currentTimeMillis() - dto.getCurrentTime()) > dto.getExpireTime()){
			return ResponseEntity.fail("短信验证码过期");
		}
		return ResponseEntity.ok("短信验证码校验通过");
	}

	@GetMapping("/info")
	public ResponseEntity info(String token){
		return userService.getUserInfo(token);
	}

	/**
	 * 更新密码
	 * @param vo
	 * @return
	 */
	@PostMapping("/password")
	public ResponseEntity modifyPwd(@RequestBody ModifyPwdVo vo) {
		return  userService.modifyPwd(vo);
	}
	/*
	忘记密码
	 */
	@PostMapping("/forget")
	public ResponseEntity forgetPwd(@RequestBody  UserRegVo user){
		return userService.forget(user);
	}

	/**
	 * 获取我的信息接口
	 */
	@GetMapping("/myInfo")
	public ResponseEntity myInfo(String token){
		return userService.getMyInfo(token);
	}

	/**
	 * 关注某人
	 * @param userId
	 * @return
	 */
	@PostMapping("/follow")
	public ResponseEntity userFollower(String userId){
		return circleService.userFollower(userId);
	}

	/**
	 * 取消关注
	 * @return
	 */
	@DeleteMapping("/follow")
	public ResponseEntity delFollower(String userId){
		return  circleService.delFollower(userId);
	}

	/**
	 * 获取关注我的人
	 * @return
	 */
	@GetMapping("/myFollowers")
	public ResponseEntity getMyFollowers(){
		return  circleService.getMyFollowers();
	}


	/**
	 * 获取我的关注人
	 * @return
	 */
	@GetMapping("/myFollow")
	public ResponseEntity getMyFollow(){
		return  circleService.getMyFollow();
	}

	/**
	 * 查看用户首页
	 * @param userId
	 * @param lat
	 * @param lng
	 * @return
	 */
	@GetMapping("/index")
	public ResponseEntity getUserIndex(String userId,Double lat, Double lng){
		return  userService.getUserIndex(userId,lat,lng);
	}

	@DeleteMapping("/album")
	public ResponseEntity deleteUserAlbum(String ids){
		return userService.deleteUserAlbum(ids);
	}


}
